package com.example.understand_compose1.testcase_activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.CheckBox
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.understand_compose1.R
import com.example.understand_compose1.databinding.ActivityFetCheckBoxBinding

class FetCheckBoxActivity : AppCompatActivity() {
    lateinit var binding: ActivityFetCheckBoxBinding


    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityFetCheckBoxBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        val cb1 = binding.cb1 as CheckBox
        val cb2 = binding.cb2 as CheckBox
        val cb3 = binding.cb3 as CheckBox
        val cb4 = binding.cb4 as CheckBox
        val bt1 = binding.bt1 as Button
        val tv2 = binding.tv2 as TextView

        bt1.setOnClickListener {
            val selectedvalue = mutableListOf<String>()

            // TODO: kotlin语法，直接写逻辑，不用实现接口？
            if (cb1.isChecked) {
                selectedvalue.add(cb1.text.toString())
                cb1.isEnabled = false
            }

            if (cb2.isChecked) {
                selectedvalue.add(cb2.text.toString())
                cb2.isEnabled = false
            }

            if (cb3.isChecked) {
                selectedvalue.add(cb3.text.toString())
                cb3.isEnabled = false
            }

            if (cb4.isChecked) {
                selectedvalue.add(cb4.text.toString())
                cb4.isEnabled = false
            }

            if (selectedvalue.isEmpty()) {
                Toast.makeText(this, "啥也没选", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            tv2.setText("你的爱好是: $selectedvalue")
        }
    }
}